/* ==================================================================== *
 *
 * This file is part of Mapics Framework.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @author Andrey Cherkashin <action.open@gmail.com>
 *
 * ==================================================================== */

/**
 * Constructs object that can be added on map overlay
 *
 * @param html (string) HTML Inner code of this object
 * @param data (JSONArray) Properties of this map object [optional] {
 *     'position': {
 *         'lat': (double) Latitude,
 *         'lng': (double) Longitude
 *     },
 *     'offset': {
 *         'x': (int) Left offset,
 *         'y': (int) Top offset
 *     }
 * }
 */
mapics.MapObject = function(html, data)
{
    this.m_Div = document.createElement("div");
    this.m_Div.innerHTML = html;
    this.m_Div.setAttribute("style", "position: absolute;");

    this.m_Data = data != null ? data : {};
    this.m_Position = {};
};

/** ------------------------------------------------------------------------- **
 *  ACTIONS
 ** ------------------------------------------------------------------------- **/

/**
 * Removes this map object from map overlay
 *
 * @return mapics.MapObject
 */
mapics.MapObject.prototype.remove = function()
{
    if( this.m_Overlay ) {
        this.m_Overlay.removeObject( this );
    }

    return this;
};

/** ------------------------------------------------------------------------- **
 *  PROPERTIES
 ** ------------------------------------------------------------------------- **/

/**
 * Returns div of this map object
 *
 * @return HTMLObject
 */
mapics.MapObject.prototype.getDiv = function() {
    return this.m_Div;
};

/**
 * Returns position of this map object
 *
 * @param provider (string) If specified, returns position, transormed for this
 *                  provider. For example, return google.maps.LatLng, if you will
 *                  choose "google".
 *                  If NOT specified, returns { 'lat', 'lng' } JSONArray object.
 *
 * @return object|{
 *     'lat': (double) Latitude,
 *     'lng': (double) Longitude
 * }
 */
mapics.MapObject.prototype.getPosition = function(provider)
{
    if( provider == null ) {
        return this.m_Data.position;
    }

    if( this.m_Position[provider] != null ) {
        return this.m_Position[provider];
    }

    switch(provider)
    {
        case "google":
            return (this.m_Position['google'] = new google.maps.LatLng(
                this.m_Data.position.lat,
                this.m_Data.position.lng
            ));
        break;

        case "yandex":
            return (this.m_Position['yandex'] = new YMaps.GeoPoint(
                this.m_Data.position.lng,
                this.m_Data.position.lat
            ));
        break;
    }
};

/**
 * Sets position of this map object
 *
 * @param position {
 *     'lat': (double) Latitude,
 *     'lng': (double) Longitude
 * }
 *
 * @return mapics.MapObject
 */
mapics.MapObject.prototype.setPosition = function(position)
{
    this.m_Data.position = position;

    // Clear array of transformed positions
    this.m_Position = {};

    // Update overlay
    if( this.m_Overlay != null ) {
        this.m_Overlay.update();
    }

    return this;
};

/**
 * Returns offset in pixels for this map object
 *
 * @return {
 *     'x': (int) Left offset,
 *     'y': (int) Top offset
 * }
 */
mapics.MapObject.prototype.getOffset = function() {
    return this.m_Data.offset;
};

/**
 * Sets offset in pixels for this map object
 *
 * @param offset {
 *     'x': (int) Left offset,
 *     'y': (int) Top offset
 * }
 *
 * @return mapics.MapObject
 */
mapics.MapObject.prototype.setOffset = function(offset) {
    this.m_Data.offset = offset;

    // Update overlay
    if( this.m_Overlay != null ) {
        this.m_Overlay.update();
    }
    
    return this;
};

/**
 * Sets current overlay of this map object
 *
 * @param mapOverlay (mapics.MapLayerOverlay<Provider>)
 *
 * @return mapics.MapObject
 */
mapics.MapObject.prototype.setOverlay = function(mapOverlay) {
    this.m_Overlay = mapOverlay;
    return this;
};

